

//William Keller (#996964933) Section 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "utils.h"

int dnapal();

int main() {
    return dnapal(); // just so we don't call main from main upon error
}

int dnapal() {
    char *seq = malloc(300); //malloc manually so we can free it later. 299 chars + \0. The reference program works at 299 characters of input, ignores input 301 characters and above, and has undefined behavior at 300 (appends gibberish to results). I have not replicated this behavior.
    printf("Input DNA string (use only a, A, c, C, g, G, t, T) or 'q' to exit:\n");
    scanf("%299s", seq); // discard input past 299 characters to leave room for the null terminator

    if ((strstr(seq, "q") != 0) && (strlen(seq) == 1)) { // exit if "q"
        return 0;
    }

    printf("________________________________________________________________\n\n"); // separator
    
    if (!is_valid(seq)) { // check we have a valid sequence
        free(seq); // don't allocate another 300 bytes every single fail.
        return dnapal(); // try again.
    }

    normalize(seq); // uppercase everything

    if (is_palindrome(seq)) { // first output
        printf("(1) This sequence is a palindrome.\n");
    } else {
        printf("(1) This sequence is not a palindrome.\n");
    }

    printf("(2) Forward sequence: %s.\n", seq); // second output
   
    rev_comp(seq);
    printf("(3) Reverse complimentary sequence: %s.\n\n", seq); // third output

    free(seq); // don't leak memory
    dnapal(); // and do it again.

    return 0; // to keep gcc happy even though control won't get here
}
